//
//  IAAMarketDetailVC.m
//  iPadAuditAuthorise
//
//  Created by XiangDongMBP on 16/9/7.
//  Copyright © 2016年 XiangDong. All rights reserved.
//

#import "IAAMarketDetailVC.h"
#import <PNChart.h>
#import "GlobalHeader.h"
#import "IAAInfoMsgModel.h"
#import "XDEmptyVCView.h"

@interface IAAMarketDetailVC ()<XDEmptyVCViewDelegate>

@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UILabel *levelLabel;
@property (weak, nonatomic) IBOutlet UILabel *scoresLabel;
@property (weak, nonatomic) IBOutlet UILabel *crmOrgNoLabel;
@property (weak, nonatomic) IBOutlet UILabel *crmManagerLabel;


@property (weak, nonatomic) IBOutlet UIView *pieChatBgView;

@property (weak, nonatomic) IBOutlet UILabel *nProductLabel;

@property (weak, nonatomic) IBOutlet UIButton *masterInfoBtn;
@property (weak, nonatomic) IBOutlet UIButton *secondInfoBtn;

@property (strong, nonatomic) PNPieChart *pieChart;
@property (strong, nonatomic) XDEmptyVCView *emptyView;
@property (strong, nonatomic) IAAMarketDetailModel *model;

@end

@implementation IAAMarketDetailVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    //设置导航栏
    [self initNavigationBar];
    //設置子Views
    [self initSubViews];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (BOOL)hidesBottomBarWhenPushed {
    return YES;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (void)initNavigationBar {
    self.navigationItem.title = @"营销信息详情";
}

- (void)initSubViews {
    self.levelLabel.layer.masksToBounds = YES;
    self.levelLabel.layer.cornerRadius = 2;
    self.scoresLabel.layer.masksToBounds = YES;
    self.scoresLabel.layer.cornerRadius = 2;
    
    // 饼状图
//    NSArray *items = @[[PNPieChartDataItem dataItemWithValue:1 color:PNRed description:@"第一块"],
//                       [PNPieChartDataItem dataItemWithValue:2 color:PNBlue description:@"第二块"],
//                       [PNPieChartDataItem dataItemWithValue:1 color:PNBrown description:@"第三块"]];
    NSArray *items = @[];
    self.pieChart = [[PNPieChart alloc] initWithFrame:CGRectMake(0, 0, 240, 240) items:items];
    [self.pieChart strokeChart];
    [self.pieChatBgView addSubview:self.pieChart];
    
    self.masterInfoBtn.tag = 1000;
    [self.masterInfoBtn addTarget:self action:@selector(infoBtnTapped:) forControlEvents:UIControlEventTouchUpInside];
    self.secondInfoBtn.tag = 1001;
    [self.secondInfoBtn addTarget:self action:@selector(infoBtnTapped:) forControlEvents:UIControlEventTouchUpInside];
    
    self.emptyView = [[[NSBundle mainBundle] loadNibNamed:@"XDEmptyVCView" owner:self options:nil] firstObject];
    self.emptyView.delegate = self;
    
    // 请求数据
    [self requestMarketDetail];
}

- (void)refreshWholeView {
    self.nameLabel.text = [NSString stringWithFormat:@"客户：%@", self.model.customerInfo.name];
    self.levelLabel.text = [NSString stringWithFormat:@"   %@   ", self.model.customerInfo.level];
    self.scoresLabel.text = [NSString stringWithFormat:@"   %@   ", self.model.customerInfo.scores];
    self.crmOrgNoLabel.text = [NSString stringWithFormat:@"CRM归属机构：%@", self.model.customerInfo.crmOrgNo];
    self.crmManagerLabel.text = [NSString stringWithFormat:@"CRM归属机构：%@", self.model.customerInfo.crmManager];
    
    //饼状图
//    NSArray<UIColor *> *colorArray = @[IAA_COLOR_0x129CFF(1.0),
//                                       IAA_COLOR_0xF8E936(1.0),
//                                       IAA_COLOR_0xFE6064(1.0),
//                                       IAA_COLOR_0x85E7F2(1.0)];
    NSArray<UIColor *> *colorArray = @[UIColorFromRGBA(0xffb442, 1.0),
                                       UIColorFromRGBA(0xff6c64, 1.0),
                                       UIColorFromRGBA(0xbd60bf, 1.0),
                                       UIColorFromRGBA(0x4281d9, 1.0),
                                       UIColorFromRGBA(0x2bbef9, 1.0),
                                       UIColorFromRGBA(0x77d859, 1.0)];
    NSMutableArray *mArray = [NSMutableArray arrayWithCapacity:self.model.asst.count];
    for (NSInteger i = 0; i < self.model.asst.count; i++) {
        IAAMarketAssetItem *item = self.model.asst[i];
        NSInteger colorIndex = i % colorArray.count;
        if (i == self.model.asst.count - 1 && colorIndex == 0) {
            colorIndex = 1;
        }
        PNPieChartDataItem *pie = [PNPieChartDataItem dataItemWithValue:[item.assetRate floatValue]
                                                                  color:colorArray[colorIndex]
                                                            description:item.assetName];
        [mArray addObject:pie];
    }
    [self.pieChart updateChartData:mArray];
    [self.pieChart strokeChart];
    
    // 未持有主推产品
    NSMutableArray * nameMArray = [NSMutableArray array];
    for (IAAMarketNProItem *nItem in self.model.nproducts) {
        [nameMArray addObject:nItem.productname];
    }
    self.nProductLabel.text = [nameMArray componentsJoinedByString:@"、"];
}

#pragma mark - <XDEmptyVCViewDelegate>代理
- (void)xDEmptyVCView:(XDEmptyVCView *)selPhotoView refreshBtnTapped:(UIButton *)btn {
    [self requestMarketDetail];
}

#pragma mark - 按键处理
- (void)infoBtnTapped:(UIButton *)btn {
    ;
}

#pragma mark - 网络请求
- (void)requestMarketDetail {
    NSString *urlStr = [IAAUrlProtocolMerge appInfoMsgDetail];
    NSDictionary *pars = @{@"infoNo" : (self.infoNo ? self.infoNo : @""),
                           @"token"  : IAA_APP_DELEGATE.token,
                           @"osName" : @"iOS",
                           @"busiId" : (self.busiId ? self.busiId : @"")};
    
    MBProgressHUD * hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [XDAFHttpRequest POST:urlStr parameters:pars progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"营销信息详情返回%@", responseObject);
        [hud hide:NO];
        self.model = [IAAMarketDetailModel yy_modelWithJSON:responseObject];
        if ([self.model.resultCode isEqualToString:IAA_RESULT_CODE_SUC]) {
            [self refreshWholeView];
            [self.emptyView hide];
        }else{
            [MBProgressHUD showPromptWithAutoHideInView:self.view message:self.model.resultMsg];
            [self.emptyView showInView:self.view];
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"营销信息详情失败%@", error);
        [hud hide:NO];
        [MBProgressHUD showPromptWithAutoHideInView:self.view message:@"网络请求失败"];
        [self.emptyView showInView:self.view];
    }];
}

@end
